import SaleBillView from '@/views/bill/BillView.vue'
import ReceiptView from '@/views/bill/ReceiptView.vue'
import FundView from '@/views/bill/FundView.vue'
import FinanceView from '@/views/business/FinanceView.vue'
import UserInfoView from '@/views/setting/UserInfoView.vue'

import { tabStore } from '@/stores/tabs' 

function menuList(){
    return [
        {
            "title":"基础表单",
            "index":"bill",
            child:[
                {"title": "销售订单", "path":"/bill/sale", "name":"SaleBill", "component": SaleBillView },
                {"title": "收款单", "path":"/bill/receipt", "name":"ReceiptBill", "component": ReceiptView},
                {"title": "资金明细", "path":"/bill/fund", "name":"FundBill", "component": FundView}
            ]
        },
        {
            "title":"业务处理",
            "index": "business",
            child:[
                {"title": "订单核销", "path":"/business/finance", "name":"Fianace", "component": FinanceView}
            ]
        },
        {
            "title":"设置",
            "index":"setting",
            child:[
                {"title": "个人设置", "path":"/setting/self", "name":"Self", "component": UserInfoView}
            ]
        }
    ]
}

export function getMenus(){
    const menus = menuList()
    let routes = []
    for(const m of menus){
        let sub: SubMenuItem = {
            "title": m.title,
            "index": m.index,
            "child": []
        }
        for(const s of m.child){
            sub.child.push({
                "path": s.path,
                "title": s.title
            })
        }
        routes.push(sub)
    }

    return routes
}

export function getRoute(){
    const menus = menuList()
    let routes = []
    for(const m of menus){
        for(const s of m.child){
            routes.push({
                path: s.path,
                name: s.name,
                component: s.component,
                beforeEnter: async(to: any)=>{
                    const store = tabStore()
                    await store.addTab(s.title, s.name, to.params, to.query, to.hash, to.path)
                }
            })
        }
    }

    routes.push({
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: ()=>import('@/views/404.vue'),
        beforeEnter: async(to:any)=>{
            const store = tabStore()
            await store.addTab('页面错误', 'NotFound', {}, {}, '', to.path)
        }
    })

    return routes 
}

interface SubMenuItem{
    title: string,
    index: string,
    child: SubMenuItemChild[]
}

interface SubMenuItemChild{
    path: string, 
    title: string
}